
package com.winning.sx.microframework.controller.base;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.winning.dapi.data.entity.DataTable;
import com.winning.sx.microframework.common.util.Constains;
import com.winning.sx.microframework.common.util.FileTools;
import com.winning.sx.microframework.common.util.Tools;
import com.winning.sx.microframework.model.exception.CustomException;
import com.winning.sx.microframework.model.message.AjaxDataResponseMessage;
import com.winning.sx.microframework.model.message.AjaxTableResponseMessage;
import com.winning.sx.microframework.service.EasyService;
import com.winning.sx.microframework.service.sysmanage.ConstService;
import com.winning.sx.microframework.service.sysmanage.RoleManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/equipment/model")
public class EquipmentModelController {
    @Autowired
    EasyService easyService;
    @Autowired
    ConstService service;

    @RequestMapping(value = {"/", ""})
    public ModelAndView init() throws IOException, CustomException, SQLException {
        ModelAndView mv = new ModelAndView("/base/equipmentModel");
        List state = service.queryCount("120");
        mv.addObject("state", state);
        Map eqRootClassP = new HashMap();
        eqRootClassP.put("superId", "0");
        eqRootClassP.put("validFlag", "100001");
        DataTable eqRootClassS = easyService.queryList("BASE_EqClass", eqRootClassP);
        List eqRootClass = eqRootClassS.getDataRows();
        mv.addObject("eqRootClass", eqRootClass);
        List brand = service.queryCount("300");
        mv.addObject("brand", brand);
        return mv;
    }

    // 资产型号查询
    @ResponseBody
    @RequestMapping(value = {"/queryList"})
    public AjaxTableResponseMessage queryMessageList(String datas) {
        AjaxTableResponseMessage ajaxTableResponseMessage = new AjaxTableResponseMessage();
        Map<String, String> param = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null)
                param = objectMapper.readValue(datas, Map.class);
            Map paramNew = new HashMap();
            if ("0".equals(param.get("types"))) {
                paramNew.put("type", null);
            } else {
                paramNew.put("type", param.get("types"));
            }
            if ("0".equals(param.get("brands"))) {
                paramNew.put("brand", null);
            } else {
                paramNew.put("brand", param.get("brands"));
            }
            String sql = "select e.*,c1.name brands,c2.name types,c2.superId types1F from BASE_EquipmentDetail e " +
                    "left join BASE_Const c1 on c1.code=e.brand " +
                    "left join base_eqClass c2 on c2.code=e.type " +
                    " where 1=1 and e.validFlag='100001'" +
                    (paramNew.get("type") != null ? (" and e.type like '" + paramNew.get("type") + "%'") : "") +
                    (paramNew.get("brand") != null ? (" and e.brand='" + paramNew.get("brand") + "'") : "") +
                    (param.get("state") != null ? (" and e.state='" + param.get("state") + "'") : "") +
                    " order by e.createTime desc";
            DataTable all = easyService.querySql(sql, paramNew);
            ajaxTableResponseMessage.setDatas(all.getDataRows());
            ajaxTableResponseMessage.setTotalSize((int) all.getTotalRowNum());
            ajaxTableResponseMessage.setSuccess(true);
        } catch (Exception e) {
            ajaxTableResponseMessage.setSuccess(false);
            ajaxTableResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxTableResponseMessage;
    }

    @ResponseBody
    @RequestMapping(value = {"/save"})
    public AjaxDataResponseMessage insert(String datas) {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                if (!"".equals(map.get("image"))) {
                    //图片流生成文件返回文件地址
                    map.put("image", FileTools.saveImage("equipmentModelPic", map.get("image")));
                }
                map.remove("types1F");
                map.put("state", Constains.EQMODEL_STATE_1);
                map.put("pym", Tools.getFirstSpell(map.get("name").toString()));
                easyService.save("BASE_EquipmentDetail", "id", map);
                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxDataResponseMessage;
    }

    @ResponseBody
    @RequestMapping(value = {"/delete"})
    public AjaxDataResponseMessage delete(String datas) {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                easyService.delete("BASE_EquipmentDetail", "id", map);
                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxDataResponseMessage;
    }

    @ResponseBody
    @RequestMapping(value = {"/saveTo"})
    @Transactional(propagation = Propagation.REQUIRED)
    public AjaxDataResponseMessage saveTo(String datas) {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                Map deleteParam = new HashMap();
                deleteParam.put("id", map.get("fromId").toString());
                easyService.delete("BASE_EquipmentDetail", "id", deleteParam);

                Map queryParam = new HashMap();
                queryParam.put("modelId", map.get("fromId").toString());
                Map updateParam = new HashMap();
                updateParam.put("modelId", map.get("toId").toString());
                updateParam.put("oldModelId", map.get("fromId").toString());
                updateParam.put("name", map.get("name").toString());
                updateParam.put("model", map.get("model").toString());
                updateParam.put("oneCode", map.get("oneCode").toString());
                updateParam.put("brandCode", map.get("brandCode").toString());
                easyService.update("BASE_Equipment", queryParam, updateParam);
                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return ajaxDataResponseMessage;
    }
}

